package com.mxun.winefox.components


import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp


@Composable
fun InputBar(
    inputText: String,
    onInputTextChange: (String) -> Unit,
    isRecording: Boolean,
    isTTSPlaying: Boolean,
    hasCachedAudio: Boolean,
    onSendClick: () -> Unit,
    onVoiceClick: () -> Unit,
    modifier: Modifier = Modifier
) {
    Column(
        modifier = modifier
            .padding(bottom = 12.dp)
    ) {
        if (isRecording) {
            StatusText("狐狐聆听中~~")
        }
        if (isTTSPlaying) {
            StatusText("狐狐说话中~~")
        }

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .windowInsetsPadding(WindowInsets.systemBars)
                .imePadding()
                .height(IntrinsicSize.Min),
            verticalAlignment = Alignment.CenterVertically
        ) {
            VoiceButton(isRecording, onVoiceClick)

            Spacer(modifier = Modifier.width(8.dp))

            InputField(inputText, onInputTextChange)

            Spacer(modifier = Modifier.width(8.dp))

            // 缓存指示器（在发送按钮前）
            if (hasCachedAudio && inputText.isNotBlank()) {
                CacheIndicator()
                Spacer(modifier = Modifier.width(4.dp))
            }

            IconSendButton(inputText, onSendClick)
        }
    }
}

@Composable
private fun CacheIndicator() {
    Icon(
        imageVector = Icons.Default.CloudDone,
        contentDescription = "语音缓存可用",
        tint = MaterialTheme.colorScheme.primary,
        modifier = Modifier
            .size(24.dp)
            .alpha(0.8f)
    )
}

@Composable
private fun StatusText(text: String) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .padding(bottom = 8.dp),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = text,
            color = MaterialTheme.colorScheme.primary,
            fontWeight = FontWeight.Bold
        )
    }
}

@Composable
private fun VoiceButton(isRecording: Boolean, onClick: () -> Unit) {
    Box(contentAlignment = Alignment.Center) {
        IconButton(
            onClick = onClick,
            modifier = Modifier
                .size(48.dp)
                .clip(CircleShape)
                .background(MaterialTheme.colorScheme.primaryContainer)
        ) {
            Icon(
                Icons.Default.Mic,
                contentDescription = "语音输入",
                tint = if (isRecording) MaterialTheme.colorScheme.primary
                else MaterialTheme.colorScheme.onPrimaryContainer
            )
        }

        if (isRecording) {
            Box(
                modifier = Modifier
                    .size(54.dp)
                    .border(
                        width = 2.dp,
                        color = MaterialTheme.colorScheme.primary,
                        shape = CircleShape
                    )
            )
        }
    }
}

@Composable
private fun InputField(text: String, onValueChange: (String) -> Unit) {
    TextField(
        value = text,
        onValueChange = onValueChange,
        placeholder = { Text("输入消息...") },
        modifier = Modifier
            .fillMaxWidth(fraction = 0.8f)
            .background(
                MaterialTheme.colorScheme.surfaceVariant,
                RoundedCornerShape(18.dp)
            )
            .padding(horizontal = 8.dp),
        colors = TextFieldDefaults.colors(
            focusedContainerColor = Color.Transparent,
            unfocusedContainerColor = Color.Transparent,
            disabledContainerColor = Color.Transparent,
            focusedIndicatorColor = Color.Transparent,
            unfocusedIndicatorColor = Color.Transparent
        ),
        singleLine = true,
        shape = RoundedCornerShape(18.dp)
    )
}

@Composable
private fun IconSendButton(inputText: String, onClick: () -> Unit) {
    IconButton(
        onClick = onClick,
        enabled = inputText.isNotBlank(),
        modifier = Modifier
            .size(48.dp)
            .clip(CircleShape)
            .background(MaterialTheme.colorScheme.primaryContainer)
    ) {
        Icon(
            Icons.Default.Send,
            contentDescription = "发送消息",
            tint = if (inputText.isNotBlank())
                MaterialTheme.colorScheme.primary
            else
                MaterialTheme.colorScheme.onSurface.copy(alpha = 0.3f)
        )
    }
}